package edu.fudan.jwxt.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.io.*;
import java.util.Properties;


@Component
@PropertySource("classpath:dataconfig.properties")
public class DataConfig {

    //是否可以选课
    public static Boolean enableSelectCourse;


    //系统运行结束时配置更新
    @PreDestroy
    public void updateData() throws IOException {
//        String filename = "dataconfig.properties";
//        String filepath = Profile.class.getClassLoader().getResource(filename).toURI().getPath();
        String filepath = "src/main/resources/dataconfig.properties";
        Properties properties = new Properties();
        BufferedReader br = null;
        BufferedWriter bw = null;
        try{
            // 从输入流中读取属性列表（键和元素对）
            br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));
            properties.load(br);
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath)));
            properties.setProperty("EnableSelectCourse", String.valueOf(enableSelectCourse));
            properties.store(bw,"update");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            br.close();
            bw.close();
        }

    }


    @Value("${EnableSelectCourse}")
    public void setEnableSelectCourse(boolean enableSelectCourse) {
        DataConfig.enableSelectCourse = enableSelectCourse;
    }
}
